package com.ux.util
{
	import com.ux.manager.StageManager;
	
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	/**
	 * @author winnie
	 */
	public class AlphaControlUtil
	{
		
		private static var _dicChangeValue:Dictionary = new Dictionary();
		private static var _dicTargetAlaph : Dictionary = new Dictionary();
		private static var _dicCompleteH : Dictionary = new Dictionary();
		private static var _dicUpdateH : Dictionary = new Dictionary();
		
		
		public static function changeAlpha( _dispObj:Object, _duration:Number = 1, _targetAlpha:Number = 1, _completeHandler:Function=null, _updateHandler:Function=null ):void
		{
			if( _dispObj == null ) 
			{
				trace(" ==> AlphaControlUtil changeAlpha : " + "디스플레이 오브젝트가 없습니다" );
				return;
			}
			
			_dispObj.removeEventListener( Event.ENTER_FRAME, onFadeInEnter );
			_dispObj.removeEventListener( Event.ENTER_FRAME, onFadeOutEnter );
			
			_dicTargetAlaph[ _dispObj ] = _targetAlpha;
			_dicChangeValue[ _dispObj ] = ( _targetAlpha-_dispObj.alpha ) / (_duration*StageManager.stage.frameRate);
			if( _completeHandler != null ) _dicCompleteH[_dispObj] = _completeHandler;
			if( _updateHandler != null ) _dicUpdateH[_dispObj] = _updateHandler;
			
			if( _dispObj.alpha > _targetAlpha ) _dispObj.addEventListener( Event.ENTER_FRAME, onFadeOutEnter );
			else _dispObj.addEventListener( Event.ENTER_FRAME, onFadeInEnter );
			
			
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		private static function onFadeInEnter( e:Event ):void
		{
			var obj : Object = e.currentTarget;
			
			if( obj.alpha < _dicTargetAlaph[ obj ] )
			{
				obj.alpha += _dicChangeValue[ obj ];
				if( _dicUpdateH[ obj ] != null ) _dicUpdateH[ obj ]();
			}
			else
			{
				if( _dicCompleteH[ obj ] != null ) _dicCompleteH[ obj ]();
				
				_dicUpdateH[ obj ] = null;
				_dicCompleteH[ obj ] = null;
				obj.removeEventListener( Event.ENTER_FRAME, onFadeInEnter );
			}
		}
		
		
		private static function onFadeOutEnter( e:Event ):void
		{
			var obj : Object = e.currentTarget;
			
			if( obj.alpha > _dicTargetAlaph[ obj ] )
			{
				obj.alpha += _dicChangeValue[ obj ];
				if( _dicUpdateH[ obj ] != null ) _dicUpdateH[ obj ]();
			}
			else
			{
				if( _dicCompleteH[ obj ] != null ) _dicCompleteH[ obj ]();
				
				_dicUpdateH[ obj ] = null;
				_dicCompleteH[ obj ] = null;
				obj.removeEventListener( Event.ENTER_FRAME, onFadeOutEnter );
			}
		}
		

		
		
		
		
		
		
		
		
		
		
		
		
		public function AlphaControlUtil(){}
	}
}